#include "VolumeSource.h"

VolumeSourceBase::VolumeSourceBase( QString text) : Source(text)
{
	m_sourceWindow = 0.0;
	m_sourceLevel = 0.0;
	m_upperThreshold=0.0;
	m_lowerThreshold=0.0;

	m_vmImageCast=NULL;
	m_vmOpacityTransferFunction=NULL;
	m_vmColorTransferFunction=NULL;
	m_vmVolumeProperty=NULL;
	m_vmVolumeRayCastCompositeFunction=NULL;
	m_vmVolumeRayCastMapper=NULL;
	m_vmVolume=NULL;
}

inline void VolumeSourceBase::setUpperThreshold(double th)
{
	m_upperThreshold = th;
	emitPropertyChanged(spThreshold);
}


inline void VolumeSourceBase::setLowerThreshold(double th)
{
	m_lowerThreshold = th;
	emitPropertyChanged(spThreshold);
}

inline void VolumeSourceBase::setThresholdBetween(double lower, double upper)
{
	m_upperThreshold = upper;
	m_lowerThreshold = lower;

	emitPropertyChanged(spThreshold);
}

inline double VolumeSourceBase::getUpperThreshold()
{
	return m_upperThreshold;
}

inline double VolumeSourceBase::getLowerThreshold()
{
	return m_lowerThreshold;
}

void VolumeSourceBase::setVolumeOpacity(QPolygonF points)
{
	if ( m_vmOpacityTransferFunction)
	{
		m_vmOpacityTransferFunction->RemoveAllPoints();

		for ( int i = 0; i < points.size(); i++)
		{
			m_vmOpacityTransferFunction->AddPoint(points.at(i).x(), points.at(i).y());	
		}

		emitPropertyChanged(spDisplayVolume);
	}
}

vtkProp* VolumeSourceBase::sourceModel( bool refresh /*= false*/ )
{
	if ( !m_vtkImage)
		return NULL;

	if ( m_vmVolume && !refresh)
		return m_vmVolume;

	m_vmImageCast = vtkImageCast::New();
	m_vmImageCast->SetInput(m_vtkImage);
	m_vmImageCast->SetOutputScalarTypeToUnsignedChar();

	m_vmOpacityTransferFunction = vtkPiecewiseFunction::New();
	m_vmColorTransferFunction = vtkColorTransferFunction::New();

	if ( m_upperThreshold == 1.0 && m_lowerThreshold == 0)
	{
		m_vmOpacityTransferFunction->AddPoint(m_lowerThreshold, 0);
		m_vmOpacityTransferFunction->AddPoint(m_upperThreshold, 0.2);

		m_vmColorTransferFunction->AddRGBPoint(m_lowerThreshold, 0.0, 0.0, 0.0);
		m_vmColorTransferFunction->AddRGBPoint(m_upperThreshold, 1.0, 97.0/255.0, 0);
	}
	else
	{
// 		m_vmOpacityTransferFunction->AddPoint(m_lowerThreshold, 0);
// 		m_vmOpacityTransferFunction->AddPoint(m_lowerThreshold/2.0, 0.1);
// 		m_vmOpacityTransferFunction->AddPoint(m_lowerThreshold/2.0 + m_upperThreshold/2.0, 0.2);
// 		m_vmOpacityTransferFunction->AddPoint(m_upperThreshold/2.0, 0.2);
// 		m_vmOpacityTransferFunction->AddPoint(m_upperThreshold, 1);

		m_vmOpacityTransferFunction->AddSegment(100,0.0, 255, 1);

		m_vmColorTransferFunction->AddRGBPoint(0, 1.0, 0.0, 0.0);
		m_vmColorTransferFunction->AddRGBPoint(64, 1.0, 1.0, 0.0);
		m_vmColorTransferFunction->AddRGBPoint(128, 0.0, 1.0, 0.0);
		m_vmColorTransferFunction->AddRGBPoint(192, 0.0, 1.0, 1.0);
		m_vmColorTransferFunction->AddRGBPoint(255, 0.0, 0.0, 1.0);
	}

	m_vmVolumeProperty = vtkVolumeProperty::New();
	m_vmVolumeProperty->SetColor(m_vmColorTransferFunction);
	m_vmVolumeProperty->SetScalarOpacity(m_vmOpacityTransferFunction);
	m_vmVolumeProperty->SetShade(0);
// 	m_vmVolumeProperty->SetAmbient(0.3);
// 	m_vmVolumeProperty->SetDiffuse(1.0);
// 	m_vmVolumeProperty->SetSpecular(0.2);
	m_vmVolumeProperty->SetInterpolationTypeToLinear();


// 	m_vmVolumeRayCastCompositeFunction = vtkVolumeRayCastCompositeFunction::New();
// 
// 	m_vmVolumeRayCastMapper = vtkVolumeRayCastMapper::New();
// 	m_vmVolumeRayCastMapper->SetVolumeRayCastFunction(m_vmVolumeRayCastCompositeFunction);
// 	m_vmVolumeRayCastMapper->SetInputConnection(m_vmImageCast->GetOutputPort());
// 	m_vmVolumeRayCastMapper->Update();

	vtkFixedPointVolumeRayCastMapper *mapper = vtkFixedPointVolumeRayCastMapper::New();
	mapper->SetBlendModeToMaximumIntensity();
	mapper->SetInputConnection( m_vmImageCast->GetOutputPort());

	m_vmVolume = vtkVolume::New();
	m_vmVolume->SetMapper(mapper);
	m_vmVolume->SetProperty(m_vmVolumeProperty);

	return m_vmVolume;
}


